﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp; 

/// -------------------------------------------
/// 来自互联网
///
/// -------------------------------------------
namespace iSun.Common.DynamicCompiler
{
    /// <summary>
    /// 动态编译文件为实体类
    /// </summary>
    public  class Compiler
    {
        /// <summary>
        /// 根据Net字符串,动态编译DLL文件
        /// </summary>
        /// <param name="dllFileName">DLL文件名称</param>
        /// <param name="source">Net字符串</param>
        /// <param name="message">消息</param>
        /// <returns>是否成功</returns>
        public static int CompilerDllFileBySource(string dllFileName, string source, ref string message)
        {
            string[] sources = new string[1];
            sources[0] = source;

            return CompilerDllFileBySource(dllFileName, sources, ref message);
        }

        /// <summary>
        /// 根据Net字符串,动态编译DLL文件
        /// </summary>
        /// <param name="dllFileName">DLL文件名称</param>
        /// <param name="sources">Net字符串数组</param>
        /// <param name="message">消息</param>
        /// <returns>是否成功</returns>
        public static int CompilerDllFileBySource(string dllFileName, string[] sources, ref string message)
        {
            int returnInt = -1;

            Microsoft.CSharp.CSharpCodeProvider objCSharpCodeProvider = new Microsoft.CSharp.CSharpCodeProvider();
            ICodeCompiler objICodeCompiler = objCSharpCodeProvider.CreateCompiler();
            CompilerParameters objCompilerParameters = new CompilerParameters();
            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            objCompilerParameters.GenerateExecutable = false;
            objCompilerParameters.GenerateInMemory = true;
            objCompilerParameters.OutputAssembly = dllFileName;

            //编译字符串
            CompilerResults cr = objICodeCompiler.CompileAssemblyFromSourceBatch(objCompilerParameters, sources);

            //判断是否成功
            if (cr.Errors.HasErrors)
            {
                returnInt = -1;

                StringBuilder errors = new StringBuilder();
                foreach (CompilerError ce in cr.Errors)
                {
                    if (errors.Length > 0)
                    {
                        errors.Append(Environment.NewLine + ce.ErrorText);
                    }
                    else
                    {
                        errors.Append(ce.ErrorText);
                    }
                }
            }
            else
            {
                returnInt = 1;
                message = "";
            }

            return returnInt;
        }

        /// <summary>
        /// 根据Net文件,动态编译DLL文件
        /// </summary>
        /// <param name="dllFileName">DLL文件名称</param>
        /// <param name="fileName">Net文件名称</param>
        /// <param name="message">消息</param>
        /// <returns>是否成功</returns>
        public static int CompilerDllFileByFile(string dllFileName, string fileName, ref string message)
        {
            string[] fileNames = new string[1];
            fileNames[0] = fileName;

            return CompilerDllFileByFile(dllFileName, fileNames, ref message);
        }

        /// <summary>
        /// 根据Net文件,动态编译DLL文件
        /// </summary>
        /// <param name="dllFileName">DLL文件名称</param>
        /// <param name="fileNames">Net文件名称数组</param>
        /// <param name="message">消息</param>
        /// <returns>是否成功</returns>
        public static int CompilerDllFileByFile(string dllFileName, string[] fileNames, ref string message)
        {
            int returnInt = -1;

            Microsoft.CSharp.CSharpCodeProvider objCSharpCodeProvider = new Microsoft.CSharp.CSharpCodeProvider();
            ICodeCompiler objICodeCompiler = objCSharpCodeProvider.CreateCompiler();
            CompilerParameters objCompilerParameters = new CompilerParameters();
            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            objCompilerParameters.GenerateExecutable = false;
            objCompilerParameters.GenerateInMemory = true;
            objCompilerParameters.OutputAssembly = dllFileName;

            //编译字符串
            CompilerResults cr = objICodeCompiler.CompileAssemblyFromFileBatch(objCompilerParameters, fileNames);

            //判断是否成功
            if (cr.Errors.HasErrors)
            {
                returnInt = -1;

                StringBuilder errors = new StringBuilder();
                foreach (CompilerError ce in cr.Errors)
                {
                    if (errors.Length > 0)
                    {
                        errors.Append(Environment.NewLine + ce.ErrorText);
                    }
                    else
                    {
                        errors.Append(ce.ErrorText);
                    }
                }
            }
            else
            {
                returnInt = 1;
                message = "";
            }

            return returnInt;
        }
    }
}
